package blue.qingyu.whimurmur.enhancement.directive;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.github.houbb.heaven.util.util.CollectionUtil;
import com.jfinal.aop.Inject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.template.Env;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import io.jboot.web.directive.annotation.JFinalDirective;
import io.jboot.web.directive.base.JbootDirectiveBase;
import io.jpress.module.article.service.ArticleService;

@JFinalDirective("articleArchiveEx")
public class ArticleArchiveExDirective extends JbootDirectiveBase {
	@Inject
	ArticleService articleService;
	
	@Override
	public void onRender(Env env, Scope scope, Writer writer) {
        List<Object[]> articleStatistics = Db.query("select date_format(ifnull(created, modified), '%Y%m') statisticDimension, count(*) amount " +
                "from article " +
                "where status = 'normal' and (style <> 'reprint' or style is null) " +
                "group by date_format(ifnull(created, modified), '%Y%m') " +
                "order by date_format(ifnull(created, modified), '%Y%m') desc");

        List<Object[]> reprintArticleStatistics = Db.query("select date_format(ifnull(created, modified), '%Y%m') statisticDimension, count(*) amount " +
                "from article " +
                "where status = 'normal' and style = 'reprint' " +
                "group by date_format(ifnull(created, modified), '%Y%m') " +
                "order by date_format(ifnull(created, modified), '%Y%m') desc");

//        JSONObject result = new JSONObject();
        Map<String, Map<String, Integer>> res = new HashMap<>();
        String month, year;
        int amount, yearAmount;

        if(CollectionUtil.isNotEmpty(articleStatistics)){
//            JSONObject monthStat = new JSONObject();
//            JSONObject yearStat = new JSONObject();
        	Map<String, Integer> monthStatMap = new TreeMap<>((k1, k2) -> Integer.parseInt(k2) - Integer.parseInt(k1));
            Map<String, Integer> yearStatMap = new TreeMap<>((k1, k2) -> Integer.parseInt(k2) - Integer.parseInt(k1));
            for(Object[] fields: articleStatistics){
                month = fields[0].toString();
                year = month.substring(0, 4);
                amount = Integer.parseInt(fields[1].toString());
                monthStatMap.put(month, amount);
                yearAmount = yearStatMap.getOrDefault(year, 0);
                yearAmount += amount;
                yearStatMap.put(year, yearAmount);
            }
//            yearStat.putAll(yearStatMap);
//            result.put("selfArticleMonthly", monthStat);
//            result.put("selfArticleYearly", yearStat);
            res.put("selfArticleMonthly", monthStatMap);
            res.put("selfArticleYearly", yearStatMap);
        }

        if(CollectionUtil.isNotEmpty(reprintArticleStatistics)){
//            JSONObject monthStat = new JSONObject();
//            JSONObject yearStat = new JSONObject();
            Map<String, Integer> monthStatMap = new TreeMap<>((k1, k2) -> Integer.parseInt(k2) - Integer.parseInt(k1));
            Map<String, Integer> yearStatMap = new TreeMap<>((k1, k2) -> Integer.parseInt(k2) - Integer.parseInt(k1));
            for(Object[] fields: reprintArticleStatistics){
                month = fields[0].toString();
                year = month.substring(0, 4);
                amount = Integer.parseInt(fields[1].toString());
                monthStatMap.put(month, amount);
                yearAmount = yearStatMap.getOrDefault(year, 0);
                yearAmount += amount;
                yearStatMap.put(year, yearAmount);
            }
//            yearStat.putAll(yearStatMap);
//            result.put("reprintArticleMonthly", monthStat);
//            result.put("reprintArticleYearly", yearStat);
            res.put("reprintArticleMonthly", monthStatMap);
            res.put("reprintArticleYearly", yearStatMap);
        }

        scope.setLocal("articleArchive", res);
        renderBody(env, scope, writer);
	}

	@Override
    public boolean hasEnd() {
        return true;
    }
}
